package com.mall.product.controller;

import com.mall.common.utils.BaseResult;
import com.mall.common.utils.PageUtils;
import com.mall.product.service.CategoryBrandRelationService;
import com.mall.product.entity.CategoryBrandRelationEntity;
import com.mall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 品牌分类关联
 *
 * @author rose
 * @email zichuanhaoyue@gmail.com
 * @date 2021-05-16 19:10:47
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    // @RequestMapping("/catelog/list")
    @GetMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public BaseResult catelogList(@RequestParam("brandId") Long brandId){

        List<CategoryBrandRelationEntity> page = categoryBrandRelationService.catelogList(brandId);

        return BaseResult.isOk(page);
    }


    /**
     * 列表
     */
    // @RequestMapping("/list")
    @GetMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public BaseResult list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return BaseResult.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public BaseResult info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return BaseResult.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public BaseResult save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return BaseResult.ok();
    }


    /**
     *  1.Cotroller 处理请求，接受和校验数据
     *  2.Service 接收 Controller 传递来的数据
     *  3. Controller 接受Service 处理完的数据，封装页面指定的Vo
     *
     *
     *
     *
     */
    @GetMapping("/brands/list")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public BaseResult relationBrandsList(@RequestParam(value = "catId",required = true) Long catId){


        List<BrandVo> list = categoryBrandRelationService.getBrandsByCatId(catId);

        return BaseResult.putData(list);
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public BaseResult update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return BaseResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public BaseResult delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return BaseResult.ok();
    }

}
